<?php
/**********************************************************************************
WIKINDX: Bibliographic Management system.
Copyright (C)

This program is free software; you can redistribute it and/or modify it under the terms 
of the GNU General Public License as published by the Free Software Foundation; either 
version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; 
if not, write to the 
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

The WIKINDX Team 2006
sirfragalot@users.sourceforge.net
**********************************************************************************/
/*****
*	CATEGORYTREE class.
*
*	$Header: /cvsroot/wikindx/wikindx3/core/browse/CATEGORYTREE.php,v 1.1 2006/07/29 00:09:54 sirfragalot Exp $
*****/
class CATEGORYTREE
{
// Constructor
	function CATEGORYTREE($db)
	{
		$this->db = $db;
		include_once("core/browse/BROWSECOMMON.php");
		$this->common = new BROWSECOMMON($this->db);
		include_once("core/browse/BROWSECATEGORY.php");
		$this->categories = new BROWSECATEGORY($this->db, FALSE);
		include_once("core/browse/BROWSEKEYWORD.php");
		$this->keywords = new BROWSEKEYWORD($this->db, FALSE);
		include_once("core/messages/MESSAGES.php");
		$this->messages = new MESSAGES();
		include_once("core/html/TABLE.php");
		include_once("core/html/MISC.php");
		include_once("core/html/HTML.php");
		$this->html = new HTML();
		include_once("core/template/TEMPLATE.php");
		$this->template = new TEMPLATE('content');
	}
	function display()
	{
		$this->template->setVar('heading', $this->messages->text("heading", "categoryTree"));
		$this->categories->sum = $this->categories->category = array();
		$this->categories->getCategories();
		$pString = $this->categories->bib ? MISC::p($this->categories->bib, "hint") : FALSE;
		$this->catSum = $this->categories->sum;
		$this->category = $this->categories->category;
		natcasesort($this->catSum);
		natcasesort($this->category);
// 3 columns in table
		$pString .= TABLE::tableStart(FALSE, 1, 0, 0, "left");
		$index = 1;
		foreach($this->category as $catId => $catName)
		{
			$this->keywords->sum = $this->keywords->keyword = array();
			$this->getKeywords($catId);
			$this->kwSum = $this->keywords->sum;
			$this->keyword = $this->keywords->keyword;
			natcasesort($this->kwSum);
			natcasesort($this->keyword);
// print_r($this->keyword); print "<P>";
			if(($index % 3) == 1)
				$pString .= TABLE::trStart();
			$catName = $this->html->codeToHtml($catName);
			$td = MISC::b(MISC::a("link", $catName, "index.php?action=listCategoryProcess" . 
				htmlentities("&id=" . $catId))) . "&nbsp;[" . $this->catSum[$catId] . "]" . MISC::br();
			if(!empty($this->keyword))
			{
				$td .= $this->messages->text("misc", "categoryTreeKeywords");
				foreach($this->keyword as $kwId => $kwName)
				{
					$td .= MISC::br() . "&nbsp;&nbsp;&nbsp;&nbsp;" . 
						MISC::a("link", $kwName, "index.php?action=listKeywordProcess" . 
						htmlentities("&id=" . $kwId) . htmlentities("&catId=" . $catId)) . 
						"&nbsp;[" . $this->kwSum[$kwId] . "]";
				}
			}
			$pString .= TABLE::td($td);
			if(!($index % 3))
				$pString .= TABLE::trEnd();
			$index++;
		}
		if(($index % 3) == 2) // 1 populated cell so add 2 blank cells
			$pString .= TABLE::td("&nbsp;") . TABLE::td("&nbsp;") . TABLE::trEnd();
		else if(!($index % 3)) // 2 populated cells so add 1 blank cell
			$pString .= TABLE::td("&nbsp;") . TABLE::trEnd();
		$pString .= TABLE::tableEnd();
		$this->template->setVar('body', $pString);
		return $this->template->process();
	}
	function getKeywords($catId)
	{
		list($condition, $bib) = $this->common->userBibCondition('WKX_resource_keyword');
		if($condition)
			$condition = " WHERE " . $condition . " AND ";
		else
			$condition = " WHERE ";
		$condition .= "FIND_IN_SET(" . $this->db->tidyInput($catId) . ", CONCAT(" . 
			$this->db->formatField('categories') . ", ','))";
		$join = " LEFT JOIN " . $this->db->formatTable('WKX_resource_category') . " ON " . 
			$this->db->formatField('WKX_resource_category.id') . "=" . 
			$this->db->formatField('WKX_resource_keyword.id');
		$recordset = $this->db->select(array('WKX_resource_keyword'), 
			array(array('WKX_resource_keyword.id' => 'keywordId'), 'keywords'), $join . $condition);
		while($row = $this->db->loopRecordSet($recordset))
			$this->keywords->collate($row, 'keywords');
	}
}
?>
